package cz.cvut.manatee.client.concordance;

import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import cz.cvut.manatee.client.RadioButtonExtended;
import cz.cvut.manatee.client.Resources;
import cz.cvut.manatee.shared.AttributItem;
import cz.cvut.manatee.shared.concordance.CollectedParameters;
import cz.cvut.manatee.shared.concordance.CollectedSortParameters;

public class SortWidget extends ConcordanceFormWidget {

	private final HorizontalPanel sortPanel;
	
	private final Label selectAttributLabel;
	private final Label selectPositionLabel;
	
	private final ListBox selectAttribut;
	private final ListBox selectPosition;

	private LinkedHashMap<String, String> attributesList;
	private LinkedHashMap<String, String> positionsList;
	
	private final CheckBox caseSensitive;
	
	private final VerticalPanel radioAscOrDesc;
	
	private final RadioButtonExtended radioAsc;
	private final RadioButtonExtended radioDesc;
	
	public SortWidget(LinkedHashMap<String, String> attributes, String defaultAttribute, LinkedHashMap<String, String> positions, String defaultPosition){
		
		sortPanel = new HorizontalPanel();
		sortPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		initWidget(sortPanel);
		
		attributesList = new LinkedHashMap<String, String>();
		selectAttribut = new ListBox();
		setAttributes(attributes, defaultAttribute);
		
		positionsList = new LinkedHashMap<String, String>();
		selectPosition = new ListBox();
		setPositions(positions, defaultPosition);
		
		selectAttributLabel = new Label("Atribut");
		selectPositionLabel = new Label("Pozice");

		caseSensitive = new CheckBox("Case sensitive");
		caseSensitive.setValue(true);

		radioAsc = new RadioButtonExtended("direction", "Vzestupně", true, true);
		radioAsc.setValue(true);
		radioDesc = new RadioButtonExtended("direction", "Sestupně", true, false);
		
		radioAscOrDesc = new VerticalPanel();
		radioAscOrDesc.add(radioAsc);
		radioAscOrDesc.add(radioDesc);
		
		
		sortPanel.add(selectAttributLabel);
		sortPanel.add(selectAttribut);
		sortPanel.add(selectPositionLabel);
		sortPanel.add(selectPosition);
		sortPanel.add(caseSensitive);
		sortPanel.add(radioAscOrDesc);
		
	}
	
	public boolean validate(){
		this.clearError();
		
		boolean validation = true;
		if( selectAttribut.getSelectedIndex() == -1 ){ selectAttribut.addStyleName(Resources.INSTANCE.css().formError()); validation = false; }
		if( selectPosition.getSelectedIndex() == -1 ){ selectPosition.addStyleName(Resources.INSTANCE.css().formError()); validation = false; }
		
		if(!radioAsc.getValue() && !radioDesc.getValue()){
			radioAsc.addStyleName(Resources.INSTANCE.css().formError());
			radioDesc.addStyleName(Resources.INSTANCE.css().formError());
			validation = false;
		}
		
		return validation;
	}
	
	public void clearError(){
		selectAttribut.removeStyleName(Resources.INSTANCE.css().formError());
		selectPosition.removeStyleName(Resources.INSTANCE.css().formError());
		radioAsc.removeStyleName(Resources.INSTANCE.css().formError());
		radioDesc.removeStyleName(Resources.INSTANCE.css().formError());
	}

	public String getAttribut(){
		return selectAttribut.getValue(selectAttribut.getSelectedIndex());
	}

	public String getPosition(){
		return selectPosition.getValue(selectPosition.getSelectedIndex());
	}

	public boolean getCaseSensitive(){
		return caseSensitive.getValue();
	}

	public boolean isAsc(){
		return radioAsc.getValue();
	}

	protected void setAttribut(String attribute){
		if (selectAttribut.getItemCount() > 0) {
			if(attribute==null){
				selectAttribut.setItemSelected(0, true);
				return;
			}
			for (int iter = 0; iter < selectAttribut.getItemCount(); iter++) {
				if (selectAttribut.getValue(iter).equals(attribute)) {
					selectAttribut.setItemSelected(iter, true);
				}
			}
		}
	}

	protected void setPosition(String position){
		if (selectPosition.getItemCount() > 0) {
			if(position==null){
				selectAttribut.setItemSelected(0, true);
				return;
			}
			for (int iter = 0; iter < selectPosition.getItemCount(); iter++) {
				if (selectPosition.getValue(iter).equals(position)) {
					selectPosition.setItemSelected(iter, true);
				}
			}
		}
	}

	protected void setCaseSensitive(boolean caseSensitive){
		this.caseSensitive.setValue(caseSensitive);
	}

	protected void setAsc(boolean asc){
		radioAsc.setValue(asc);
		radioDesc.setValue(!asc);
	}

	public void setAttributes(LinkedHashMap<String, String> attributes, String defaultAttribute){
		attributesList.clear();
		attributesList.putAll(attributes);

		attributesList.clear();
		
		for(Map.Entry<String, String> attribut : attributes.entrySet()){
			selectAttribut.addItem(attribut.getValue(), attribut.getKey());
		}

		setAttribut(defaultAttribute);
	}

	public void setPositions(LinkedHashMap<String, String> positions, String defaultPosition){
		positionsList.clear();
		positionsList.putAll(positions);

		selectPosition.clear();
		
		for(Map.Entry<String, String> attribut : positions.entrySet()){
			selectPosition.addItem(attribut.getValue(), attribut.getKey());
		}

		setPosition(defaultPosition);
	}

	public void setAttributes(AttributItem[] attributs) {
		attributesList.clear();
		selectAttribut.clear();
		
		for(AttributItem attribute : attributs){
			attributesList.put(attribute.getAttributName(), attribute.getTitle());
			selectAttribut.addItem(attribute.getAttributName(), attribute.getTitle());
		}
	}

	public CollectedParameters getParameters() {
		return new CollectedSortParameters(getAttribut(), getPosition(), getCaseSensitive(), isAsc());
	}

	public void setParameters(CollectedParameters params) {
		setAttribut(((CollectedSortParameters)params).getAttribute());
		setPosition(((CollectedSortParameters)params).getPosition());
		setAsc(((CollectedSortParameters)params).isAsc());
		setCaseSensitive(((CollectedSortParameters)params).isCaseSensitive());
	}

}
